package com.framework.SystemPage
{
	import com.eco.*;
	import com.framework.BasicBoard;
	import com.framework.BasicButton;
	import com.framework.BasicPage;
	import com.framework.ButtonInfo;
	import com.framework.CustomEventButton;
	import com.framework.GameDataController;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.*;
	import flash.ui.Keyboard;
	
	public class GamePage extends BasicPage
	{
		public var btnBack:BasicButton;
		public var btnPause:BasicButton;
		
		public var myStageManager:StageManager;
		
		public var mcInfoBanner:resInfoBanner;
		public var txtStageNum:TextField;
		public var txtLevelNum:TextField;
		public var txtEnemyInfo:TextField;
		public var txtfmtMyFMT:TextFormat;
		
		public var stage_id:int;
		public var level_id:int;
		
		public var isPaused:Boolean = false;
		
		
		//test bg
		
		public function GamePage()
		{
			super();
			myStageManager = new StageManager();
			setInfo();
			
			
			
		}
		public function setStartStageId(stage_id:int,level_id:int):void
		{
			this.stage_id = stage_id;
			this.level_id = level_id;
		}
		
		public function setStartStageListener(e:CustomEventButton)
		{
			
			if(CustomEventButton.STAGE_ID == e.type)
			{
				this.stage_id = e.s_id;
				this.level_id = e.l_id;
				if(myStageManager!=null)
				{
				//	myStageManager.Clear();
				}
				setStartStageId(stage_id,level_id);
			}
		}
		override public function init(e:Event):void
		{
			super.init(e);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyboardEventListener);
			
			//newStageManager = new GameStage( 0, 0);
			GameDataController.getInstance().stage_id = stage_id;
			GameDataController.getInstance().level_id = level_id;
			
			myStageManager.StartStage(stage_id,level_id);
			this.addChild(myStageManager);
				
			//kickoff game
		//	btnBack = addButtonByName(ButtonInfo.BUTTON_STAGE_MODE,0);
		//	btnPause = addButtonByName(ButtonInfo.BUTTON_PAUSE,1);
			
			addInfoToStage();
		}
		
		override public function update(e:Event):void
		{
			PrintStageInfo();
		}
		
		private function PrintStageInfo():void
		{
			var s:String;
			if( stage_id<=9 )
				s = "0" + stage_id.toString();
			else
				s = stage_id.toString();
			txtStageNum.text = s;
			
			if( level_id<=9 )
				s = "0" + level_id.toString();
			else
				s = level_id.toString();
			txtLevelNum.text = s;
			
			var s2:String;
			if( GameDataController.getInstance().killedEnemies <= 9 )
				s = "0" + GameDataController.getInstance().killedEnemies;
			else
				s = GameDataController.getInstance().killedEnemies.toString();
			
			if( GameDataController.getInstance().totalEnemies <= 9 )
				s2 = "0" + GameDataController.getInstance().totalEnemies;
			else
				s2 = GameDataController.getInstance().totalEnemies.toString();
			txtEnemyInfo.text = s+"/"+s2;
		}
		
		private function setInfo():void
		{
			txtfmtMyFMT = new TextFormat("Verdana",11,0xffb500,true);
			mcInfoBanner = new resInfoBanner();
			mcInfoBanner.x = -20;
			mcInfoBanner.y = 250;
			txtStageNum = new TextField();
			txtStageNum.defaultTextFormat = txtfmtMyFMT;
			txtStageNum.textColor = 0x50ac23;
			txtStageNum.x = 78;
			txtStageNum.y = 316;
			
			txtLevelNum = new TextField();
			txtLevelNum.defaultTextFormat = txtfmtMyFMT;
			txtLevelNum.textColor = 0xffb500;
			txtLevelNum.x = 133;
			txtLevelNum.y = 316;
			
			txtEnemyInfo = new TextField();
			txtEnemyInfo.defaultTextFormat = txtfmtMyFMT;
			txtEnemyInfo.textColor = 0xffb500;
			txtEnemyInfo.x = 40;
			txtEnemyInfo.y = 340;

		}
		
		private function addInfoToStage():void{
			this.addChild(mcInfoBanner);
			this.addChild(txtEnemyInfo);
			this.addChild(txtStageNum);
			this.addChild(txtLevelNum);
		}

		private function KeyboardEventListener(e:KeyboardEvent)
		{
			//trace("ESC DOWN");
			if(e.keyCode == Keyboard.ESCAPE && isPaused != true)
			{
				dispatchEvent(new CustomEventButton(CustomEventButton.BUTTON_ID,ButtonInfo.BUTTON_PAUSE,0));
				isPaused = true;
			}
			
		}
	}
}